#include "qsectorwidget.h"


#include "../../libs/shared/property/qabstractproperty.h"
#include "../../libs/shared/qpropertyfactory.h"
#include "../../libs/shared/qcommonstruct.h"
#include "../../libs/shared/qhostfactory.h"

#include <QPainter>
#include <QGradient>

QSectorWidget::QSectorWidget(QWidget *parent) :
    QAbstractGraphicsWidget(parent)
{
}

void QSectorWidget::setStartAngle(int startAngle)
{
    if(startAngle<0)
    {
        startAngle=0;
    }
    if(startAngle>360)
    {
        startAngle=360;
    }
    if(m_start_angle!=startAngle)
    {
        m_start_angle=startAngle;
        update();
    }
}

int QSectorWidget::startAngle()
{
    return m_start_angle;
}

void QSectorWidget::setSpanAngle(int spanAngle)
{
    if(spanAngle<0)
    {
        spanAngle=0;
    }
    if(spanAngle>360)
    {
        spanAngle=360;
    }
    if(m_span_angle!=spanAngle)
    {
        m_span_angle=spanAngle;
        update();
    }
}

int QSectorWidget::spanAngle()
{
    return m_span_angle;
}

void QSectorWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing,m_antialiasing);
    QGradient g=property("fill_color").value<QGradient>();
    p.setPen(m_pen);
    p.setBrush(QBrush(g));
    QRect re=this->rect();
    p.drawPie(re.adjusted(m_pen_info.m_width/2+1,
                          m_pen_info.m_width/2+1,
                          -1-m_pen_info.m_width/2,
                          -1-m_pen_info.m_width/2),
              m_start_angle*16,m_span_angle*16);
}


QSectorWidgetHost::QSectorWidgetHost(QAbstractHost *parent):
    QAbstractGraphicsHost(parent)
{

}

void QSectorWidgetHost::create_object()
{
    m_object=new QSectorWidget;
    m_object->setObjectName("pieWidget");
}

QString QSectorWidgetHost::get_show_name()
{
    return tr("Pie");
}

QString QSectorWidgetHost::get_show_group()
{
    return tr("Base Graphics");
}

QString QSectorWidgetHost::get_show_icon()
{
    return ":/base_grahpics_images/sector.png";
}

void QSectorWidgetHost::init_property()
{
    QAbstractGraphicsHost::init_property();

    QAbstractProperty* pro;

    QSectorWidget* wid=(QSectorWidget*)m_object;

    pro=QPropertyFactory::create_property("Number");
    if(pro!=NULL)
    {
        pro->set_property("name","start_angle");
        pro->set_attribute("show_name",tr("Start Angle"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("start_angle",30);
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Number");
    if(pro!=NULL)
    {
        pro->set_property("name","span_angle");
        pro->set_attribute("show_name",tr("Span Angle"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("span_angle",120);
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","fill_color");
        pro->set_attribute("show_name",tr("Fill Color"));
        pro->set_attribute("group","Attributes");
        QLinearGradient g;
        g.setColorAt(0,QColor(0,0,0,0));
        g.setColorAt(1,QColor(0,0,0,0));
        m_object->setProperty("fill_color",QVariant::fromValue<QGradient>(g));
        pro->set_attribute(ATTR_CAN_SAME,true);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    set_property_value("geometry",QRect(0,0,100,100));
}

void QSectorWidgetHost::setStartAngle(int startAngle)
{
    set_property_value("start_angle",startAngle);
}

int QSectorWidgetHost::startAngle()
{
    return get_property_value("start_angle").toInt();
}

void QSectorWidgetHost::setSpanAngle(int spanAngle)
{
    set_property_value("span_angle",spanAngle);
}

int QSectorWidgetHost::spanAngle()
{
    return get_property_value("span_angle").toInt();
}

qRegisterHost("basegraphics.sector",QSectorWidgetHost,5060)
